package securityproject;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XSLTransformer {

	/**
	 * @param filename
	 * @param dfactory
	 * @throws ParserConfigurationException
	 * @throws TransformerFactoryConfigurationError
	 * @throws SAXException
	 * @throws IOException
	 * @throws TransformerConfigurationException
	 * @throws TransformerException
	 * @throws FileNotFoundException
	 */
	public static void ruleFilter(InputStream fromStream, OutputStream toStram,
			String ruleFileName) throws ParserConfigurationException,
			TransformerFactoryConfigurationError, SAXException, IOException,
			TransformerConfigurationException, TransformerException,
			FileNotFoundException {
		DocumentBuilder dBuilder;
		// Instantiate a DocumentBuilderFactory.
		javax.xml.parsers.DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
				.newInstance();
		dBuilder = dfactory.newDocumentBuilder();

		TransformerFactory tFactory = TransformerFactory.newInstance();
		// Use the DocumentBuilder to parse the XML input.
		Document inDoc = dBuilder.parse(fromStream);

		// Generate a Transformer.
		Transformer transformer = tFactory.newTransformer(new StreamSource(
				ruleFileName));

		transformer.transform(new DOMSource(inDoc), new StreamResult(toStram));
	}

}
